<template>
  <div class="aimodeltc-edit">
    <el-dialog v-bind="$attrs" :title="state.dialogTitle" v-model="state.modalVisible" :before-close="cancelDialog"
      :append-to-body="true" :destroy-on-close="true" :close-on-click-modal="false" :close-on-press-escape="false"
      :lock-scroll="false" class="aimodel-dialog" draggable>
      <el-form ref="aimodeltcRef" :model="state.form" :rules="state.rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="aimodeltcname">
              <el-input v-model="state.form.aimodeltcname" placeholder="名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模型名称" prop="aitaimodeltcmodelnameype">
              <el-input v-model="state.form.aimodeltcmodelname" placeholder="模型名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="定制价格" prop="aimodeltcdzprice">
              <el-input v-model="state.form.aimodeltcdzprice" placeholder="定制价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单条价格" prop="aimodeltcnumprice">
              <el-input v-model="state.form.aimodeltcnumprice" placeholder="单条价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否可用" prop="aimodeltcstatus">
              <el-input v-model="state.form.aimodeltcstatus" placeholder="是否可用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大token" prop="aimodeltcmaxtoken">
              <el-input v-model="state.form.aimodeltcmaxtoken" placeholder="最大token" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联key流水号" prop="aimodeltcaikeyid">
              <el-input v-model="state.form.aimodeltcaikeyid" placeholder="关联key流水号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上下文限制数" prop="aimodeltciscontextnum">
              <el-input v-model="state.form.aimodeltciscontextnum" placeholder="上下文限制数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上下文是否启用" prop="aimodeltciscontext">
              <el-input v-model="state.form.aimodeltciscontext" placeholder="上下文是否启用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回答相关性" prop="aimodeltctemperature">
              <el-input v-model="state.form.aimodeltctemperature" placeholder="回答相关性" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首次免费文件大小" prop="aimodeltcfirstmfwjsize">
              <el-input v-model="state.form.aimodeltcfirstmfwjsize" placeholder="首次免费文件大小" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大文件大小限制" prop="aimodeltcwjmaxsize">
              <el-input v-model="state.form.aimodeltcwjmaxsize" placeholder="最大文件大小限制" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单次最大投喂字数" prop="aimodeltcwztwmaxlen">
              <el-input v-model="state.form.aimodeltcwztwmaxlen" placeholder="单次最大投喂字数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文字投喂计量" prop="aimodeltcwztwjl">
              <el-input v-model="state.form.aimodeltcwztwjl" placeholder="文字投喂计量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文字投喂价格" prop="aimodeltcwztwprice">
              <el-input v-model="state.form.aimodeltcwztwprice" placeholder="文字投喂价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文字擦除计量" prop="aimodeltcwzccjl">
              <el-input v-model="state.form.aimodeltcwzccjl" placeholder="文字擦除计量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文字擦除价格" prop="aimodeltcwzccprice">
              <el-input v-model="state.form.aimodeltcwzccprice" placeholder="文字擦除价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件投喂计量" prop="aimodeltcwjtwjl">
              <el-input v-model="state.form.aimodeltcwjtwjl" placeholder="文件投喂计量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件投喂价格" prop="aimodeltcwjtwprice">
              <el-input v-model="state.form.aimodeltcwjtwprice" placeholder="文件投喂价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件擦除计量" prop="aimodeltcwjccjl">
              <el-input v-model="state.form.aimodeltcwjccjl" placeholder="文件擦除计量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件擦除价格" prop="aimodeltcwjccprice">
              <el-input v-model="state.form.aimodeltcwjccprice" placeholder="文件擦除价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="助手是否启用" prop="aimodeltcisassistantsstatus">
              <el-input v-model="state.form.aimodeltcisassistantsstatus" placeholder="助手是否启用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联助手id" prop="aimodeltcisassistantsid">
              <el-input v-model="state.form.aimodeltcisassistantsid" placeholder="关联助手id" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useAImodeltcManageStore from '@store/modules/aimodeltcManage'
import { filterObject } from '@/utils/index'
import { nextTick } from 'vue';

const aimodeltcManageStore = useAImodeltcManageStore()
const { proxy } = getCurrentInstance()
const aimodeltcRef = ref(null)

const dictsData = ref(proxy.useDict('OpenAI-Version'))

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    aimodeltcname: '',
    aimodeltcmodelname: '',
    aimodeltcdzprice: '',
    aimodeltcnumprice: '',
    aimodeltcstatus: '',
    aimodeltcmaxtoken: '',
    aimodeltcaikeyid: '',
    aimodeltciscontextnum: '',
    aimodeltciscontext: '',
    aimodeltctemperature: '',
    aimodeltcfirstmfwjsize: '',
    aimodeltcwjmaxsize: '',
    aimodeltcwztwmaxlen: '',
    aimodeltcwztwjl: '',
    aimodeltcwztwprice: '',
    aimodeltcwzccjl: '',
    aimodeltcwzccprice: '',
    aimodeltcwjtwjl: '',
    aimodeltcwjtwprice: '',
    aimodeltcwjccjl: '',
    aimodeltcwjccprice: '',
    aimodeltcisassistantsstatus: '',
    aimodeltcisassistantsid: '',
  },
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  },
})

const emits = defineEmits(['refresh'])

const getDataView = (aimodeltcid) => {
  nextTick(async () => {
    const result = await aimodeltcManageStore.view({ "aimodeltcid": aimodeltcid })
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  aimodeltcRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.aimodeltcid = state.editView.aimodeltcid
        await aimodeltcManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await aimodeltcManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      aimodeltcRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  aimodeltcRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data.aimodeltcid)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.aimodeltcname}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
